package com.abkj.platform.tail.util;

import java.io.PrintStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil
{
  public static final String DATE_FORMAT_YYYY = "yyyy";
  public static final String DATE_FORMAT_YYYYMM = "yyyyMM";
  public static final String DATE_FORMAT_YYYYMMDD = "yyyyMMdd";
  public static final String DATE_FORMAT_YYYY_MM_DD = "yyyy-MM-dd";
  public static final String DATE_FORMAT_YYYY_MM_DD2 = "yyyy/MM/dd";
  public static final String DATE_TIME_FORMAT_YYYYMMDDHHMISS = "yyyyMMddHHmmss";
  public static final String DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS = "yyyy-MM-dd HH:mm:ss";
  public static final String _DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS = "yyyy/MM/dd HH:mm:ss";
  public static final String DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI = "yyyy-MM-dd HH:mm";
  public static final String DATE_TIME_FORMAT_TRANS = "yyMMddHHmm";
  public static final String TIME_FORMAT_HH_MI_SS = "HH:mm:ss";
  public static final String TIME_FORMAT_HH_MI = "HH:mm";

  public static int deadDate(int days)
  {
    return Integer.parseInt(deadline(days));
  }

  public static String deadline(int days)
  {
    Date date = getCurDate();
    long l = date.getTime();
    l += 86400000L * days;
    return getDate2String(new Date(l));
  }

  public static Date getCurDate()
  {
    return new Date();
  }

  public static String getCurDateStr()
  {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    return sdf.format(getCurDate());
  }

  public static String getCurDateStr(String format)
  {
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    return sdf.format(getCurDate());
  }

  public static String getDate2String(Date date)
  {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    return sdf.format(date);
  }

  public static String getDate2String(Date date, String format)
  {
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    return sdf.format(date);
  }

  public static Date strToDate(String strDate, String format)
  {
    SimpleDateFormat dateFormat = new SimpleDateFormat(format);
    Date date = null;
    try
    {
      date = dateFormat.parse(strDate);
    } catch (ParseException e) {
      e.printStackTrace();
    }

    return date;
  }

  public static Date strToDateTime(String strDateTime, String fromat)
  {
    SimpleDateFormat dateTimeFormat = new SimpleDateFormat(fromat);
    Date dateTime = null;
    try {
      dateTime = dateTimeFormat.parse(strDateTime);
    } catch (ParseException e) {
      e.printStackTrace();
    }

    return dateTime;
  }

  public static String dateToStr(Date date, String format)
  {
    SimpleDateFormat dateFormat = new SimpleDateFormat(format);
    return dateFormat.format(date);
  }

  public static String dateTimeToStr(Date date, String format)
  {
    SimpleDateFormat dateTimeFormat = new SimpleDateFormat(format);
    return dateTimeFormat.format(date);
  }

  public static Date getTodayFirstTime(String today)
  {
    String todayfirstTime = today + " 00:00:00";
    return strToDateTime(todayfirstTime, 
      "yyyy-MM-dd HH:mm:ss");
  }

  public static Date getTodayLastTime(String today)
  {
    String todayLastTime = today + " 23:59:59";
    return strToDateTime(todayLastTime, 
      "yyyy-MM-dd HH:mm:ss");
  }

  public static int getTodayWeek()
  {
    Calendar cal = Calendar.getInstance();
    int w = cal.get(7) - 1;
    if (w == 0) {
      return 7;
    }

    return w;
  }

  public static int getWeek(Date date)
  {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    int w = cal.get(7) - 1;
    if (w == 0) {
      return 7;
    }
    return w;
  }

  public static boolean isToday(Date date)
  {
    String time = dateTimeToStr(date, "yyyyMMdd");
    String today = dateTimeToStr(new Date(), "yyyyMMdd");
    return time.equals(today);
  }

  public static int getNowNextHour() {
    Calendar cal = Calendar.getInstance();
    int hour = cal.get(11);
    return hour + 1;
  }

  public static boolean compareDateEquals(Date date1, Date date2)
  {
    long d1 = date1.getTime();
    long d2 = date2.getTime();
    return d1 == d2;
  }

  public static long strToLong(String strDateTime, String format)
  {
    SimpleDateFormat dateTimeFormat = new SimpleDateFormat(format);
    Date dateTime = null;
    try {
      dateTime = dateTimeFormat.parse(strDateTime);
    } catch (ParseException e) {
      e.printStackTrace();
      return 0L;
    }
    return dateTime.getTime();
  }

//  public static long longToLong(long strDateTime, String format)
//  {
//    SimpleDateFormat dateTimeFormat = new SimpleDateFormat(format);
//    Date dateTime = null;
//    try {
//      dateTime = dateTimeFormat.parse(strDateTime);
//    } catch (ParseException e) {
//      return 0L;
//    }
//    return dateTime.getTime();
//  }

  public static void main(String[] args)
  {
    System.out.println(dateTimeToStr(new Date(), "yyyy-MM-dd HH:mm:ss"));
    System.out.println(dateTimeToStr(strToDateTime("2012-08-22 15:25:24", "yyyy-MM-dd HH:mm:ss"), "yyyy-MM-dd HH:mm:ss"));

    System.out.println(dateToStr(getTodayFirstTime("2012-09-18"), "yyyy-MM-dd HH:mm:ss"));
    System.out.println(dateToStr(getTodayLastTime("2012-09-18"), "yyyy-MM-dd HH:mm:ss"));
    System.out.println(dateToStr(getTodayLastTime(dateToStr(new Date(), "yyyy-MM-dd")), "yyyy-MM-dd HH:mm:ss"));
    getTodayWeek();
    isToday(new Date());

    System.out.println(getNowNextHour());
  }
}